package com.dw.widget.slideshow.config;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.dw.widget.slideshow.R;
import com.dw.widget.slideshow.config.view.HeaderView;
import com.dw.widget.slideshow.config.view.HeaderView.OnBackButtonClickListener;
import com.dw.widget.slideshow.config.view.list.ListBlockView;
import com.dw.widget.slideshow.config.view.list.ListCheckItemView;
import com.dw.widget.slideshow.config.view.list.ListCheckItemView.OnCheckBoxSwitchListener;
import com.dw.widget.slideshow.config.view.list.ListCheckItemView.OnListItemClickListener;
import com.dw.widget.slideshow.config.view.list.ListNextItemView;
import com.dw.widget.slideshow.db.helper.ScanFolderHelper;
import com.dw.widget.slideshow.db.object.ScanFolderData;
import com.dw.widget.slideshow.utils.ApplicationEx;
import com.dw.widget.slideshow.utils.db.DataObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class ChooseFolderActivity extends Activity implements OnItemClickListener{

	private final ApplicationEx _APP = ApplicationEx.INSTANCE();
	private String fullPath;
	private File[] dirs;
	
	private HeaderView headerView;
	private ListBlockView listBlock;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		fullPath = intent.getStringExtra(_APP.INTENT_MSG_FULLPATH);
		
		setContentView(R.layout.choose_folder);
		findViewsById();
		initViews();
	}
	
	private void findViewsById()
	{
		headerView = (HeaderView)findViewById(R.id.choosefolder_header);
		listBlock = (ListBlockView)findViewById(R.id.choosefolder_block);
	}
	
	private void initViews()
	{
		headerView.setOnBackButtonClickListener(new OnBackButtonClickListener() {
			
			@Override
			public void onButtonClick(View v) {
				
				ChooseFolderActivity.this.finish();
			}
		});
		
		if(fullPath == null || "".equals(fullPath))
		{
			return;
		}
		
		File path = new File(fullPath);
		if(path.exists() == false || path.isDirectory() == false)
		{
			return;
		}
		
		dirs = path.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String filename) {

				String folderPath = String.format("%1$s/%2$s", fullPath, filename);
				File file = new File(folderPath);
				if(file.isDirectory() == false || filename.startsWith(".") || filename.equals("DCIM"))   // 不显示隐藏目录
				{
					return false;
				}
				
				return true;
			}
		});
		
		Arrays.sort(dirs, new FileNameComparator());   // 按文件夹名称进行排序

		ScanFolderHelper dbHelper = new ScanFolderHelper(ChooseFolderActivity.this, _APP.DB_NAME, ScanFolderData.class);
		for(File dir : dirs)
		{
			final String dirName = dir.getName();
			final String folderPath = String.format("%1$s/%2$s", fullPath, dirName);
			
			ListCheckItemView chooseFoldersItem = (ListCheckItemView) LayoutInflater.from(this).inflate(R.layout.listitem_check, null);
	    	chooseFoldersItem.setText(dirName);
	    	chooseFoldersItem.setBackgroundColor(0xFFEBEBEB);
	    	chooseFoldersItem.setIcon(R.drawable.icon_document01);
	    	
	    	// 设置选中状态
	    	ScanFolderData condition = new ScanFolderData();
	    	condition.setFolder(folderPath);
	    	condition.setSource(_APP.SOURCE_FOLDER);
	    	List<DataObject> fs = dbHelper.query(condition);
	    	if(fs != null && fs.size() > 0)
	    	{
	    		chooseFoldersItem.setChecked(true);
	    	}
	    	else
	    	{
	    		chooseFoldersItem.setChecked(false);
	    	}
	    	
	    	chooseFoldersItem.setOnCheckBoxSwitchListener(new OnCheckBoxSwitchListener() {
				
				@Override
				public void OnSwitch(boolean on) {

					
					ScanFolderHelper dbHelper = new ScanFolderHelper(ChooseFolderActivity.this, _APP.DB_NAME, ScanFolderData.class);

					// 保存至数据库
					if(on)
					{
						ScanFolderData data = new ScanFolderData();
						data.setFolder(folderPath);
						data.setName(dirName);
						data.setScaned(_APP.NO);
						data.setSource(_APP.SOURCE_FOLDER);
						
						dbHelper.insert(data);
					}
					else   // 从数据库删除
					{
						ScanFolderData data = new ScanFolderData();
						data.setFolder(folderPath);
						dbHelper.delete(data);
					}
				}
			});
	    	
	    	final File fDir = dir;
	    	chooseFoldersItem.setOnListItemClickListener(new OnListItemClickListener() {
				
				@Override
				public void OnClick(ListCheckItemView view) {
					
					File dir = fDir;
					String dirPath = dir.getAbsolutePath();
					
					Intent intent = new Intent(ChooseFolderActivity.this, ChooseFolderActivity.class);
					intent.putExtra(_APP.INTENT_MSG_FULLPATH, dirPath);
					startActivity(intent);
				}
			});
	    	
	    	listBlock.getAdapter().getListItems().add(chooseFoldersItem);
		}
		
		listBlock.getAdapter().notifyDataSetChanged();
		
		//listBlock.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		
		File dir = dirs[arg2];
		String dirPath = dir.getAbsolutePath();
		
		Intent intent = new Intent(this, ChooseFolderActivity.class);
		intent.putExtra(_APP.INTENT_MSG_FULLPATH, dirPath);
		startActivity(intent);
	}

	public class FileNameComparator implements Comparator<File> {

		@Override
		public int compare(File object1, File object2) {

			String name1 = object1.getName().toUpperCase();
			String name2 = object2.getName().toUpperCase();
			
			return name1.compareTo(name2);
		}
		
	}
}
